package attack.nartan;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Rectangle;

import framework.AnimationManager;
import framework.BasicFighter;
import framework.Util;
import framework.attack.AnimationAttack;
import framework.movements.LinearMovement;
import framework.movements.PropulsionMovement;

public class UpStrike extends AnimationAttack {

	protected Image upForward;

	protected Image downForward;

	protected Image upBackward;

	protected Image downBackward;

	protected Image inUseUp;
	protected Image inUseDown;
	
	protected BasicFighter target;
	
	protected boolean completed;

	public UpStrike(BasicFighter fighter) {
		super(fighter);
		AnimationManager n = fighter.getAnimations();
		this.spriteSheet = n.getSpriteSheet();
		Image[] frames = new Image[1];
		frames[0] = this.spriteSheet.getSprite(0, 2);
		this.attackAnimation = new Animation(frames, 200);
		this.attackAnimation.setLooping(false);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackAnimationBackward.setLooping(false);
		
		this.upForward = fighter.getAnimations().getSpriteSheet().getSprite(2, 5);
		this.downForward = fighter.getAnimations().getSpriteSheet().getSprite(1, 5);
		this.upBackward = this.upForward.getFlippedCopy(true, false);
		this.downBackward = this.downForward.getFlippedCopy(true, false);
		this.isJumpingAuthorized = false;
	}

	@Override
	public void trigger() {
		super.trigger();
		if (getAttacker().isForward()) {
			this.inUseUp = this.upForward;
			this.inUseDown = this.downForward;
		} else {
			this.inUseUp = this.upBackward;
			this.inUseDown = this.downBackward;
		}
		target = null;
		completed = false;
		attackAnimationUsed.setAutoUpdate(true);
	}

	@Override
	public void draw(Graphics g) {
		BasicFighter a = getAttacker();
		if (attackAnimationUsed.isStopped()) {
			this.inUseUp.draw(a.getX(), a.getY() - a.getHeight() + 8 * getScale(), a.getWidth(), a.getHeight());
			this.inUseDown.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight());
		} else {
			attackAnimationUsed.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight());
		}
	}

	@Override
	public int getDuration() {
		return 600;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		if (attackAnimationUsed.isStopped() && !completed) {
			BasicFighter a = getAttacker();
			int forward = a.isForward() ? 1 : -1;
			//LinearMovement m = new LinearMovement(a, new Point(a.getX() + 50 * getScale() * forward, a.getY() - 30 * getScale()));
			//m.setDuration(100);
			//a.move(m);
			//a.interruptJump();
			PropulsionMovement m = new PropulsionMovement(a, 100 * forward, -1500);
			m.setDuration(500);
			a.move(m);
			completed = true;
		}
	}

	@Override
	public boolean isBlocking() {
		return isActive() && currentTime < 500;
	}

	@Override
	public Rectangle getHitBox() {
		Rectangle r = getAttacker().getHitBox();
		int height = (int) (r.getHeight() / 4);
		int x = (int) (inUseUp == upForward ? r.getX() + r.getWidth() * 2 : r.getX() - r.getWidth() * 2);
		return attackAnimationUsed.isStopped() ? new Rectangle(x, r.getY() - height, r.getWidth(), height * 2) : new Rectangle(-230, -230, 0, 0);
	}

	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		if (!hasHit) {
			if (f.isAttacking() && f.getCurrentAttack().isInterruptable()) {
				f.getCurrentAttack().interrupt();
			}
			int forward = f.isForward() ? -1 : 1;
			PropulsionMovement m = new PropulsionMovement(f, 300 * forward, -1800);
			m.setDuration(500);
			f.move(m);
			f.hit(getPower(), m.getDuration());
			hasHit = true;
			this.hitSound.play();
		}
	}
	
	@Override
	public void onRelease() {
	}
}
